﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MxTol.UI.Helpers;
using MxTol.Managers;
using MxTol.Models;
using System.Configuration;
using System.Text.RegularExpressions;

namespace MxTol.UI.Controllers
{
    /// <summary>
    /// 财经法规单选题题库管理控制器
    /// </summary>
    public class FrSingleManagementController : Controller
    {
        private const string MIME_TYPE = "application/json;charset=utf-8;";

        #region View

        // 添加单选题页面
        public ActionResult AddSingleView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                return View();
            }
        }

        // 修改单选题页面
        public ActionResult UpdateSingleView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                string id = Request["id"];
                FrSingle single = null;
                try
                {
                    single = FrSingle.GetSingleByID(new Guid(id));
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLogAsync("修改财经法规单选题信息Action失败", ex);
                }

                if (single != null)
                {
                    single.Title = MethodHelper.ToOneLine(single.Title);
                    return View(single);
                }
                else
                {
                    return Content("获取题目信息失败");
                }
            }
        }

        // 查询单选题页面
        public ActionResult QuerySingleView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                return View();
            }
        }

        #endregion

        #region 功能

        // 以Json分页格式返回所有单选题
        public ActionResult GetFrSingleJson()
        {
            string json = EasyuiHelper.DefaultJson;
            try
            {
                string currentPage = Request["page"];
                string pageSize = Request["rows"];

                json = FrSingleManager.GetAllSingle(int.Parse(currentPage), int.Parse(pageSize));
            }
            catch (Exception ex)
            {
                json = EasyuiHelper.DefaultJson;
                LogHelper.WriteLogAsync("获取财经法规单选题题库Json格式数据错误", ex);
            }
            return Content(json, MIME_TYPE);
        }

        // 添加单选题
        public ActionResult AddSingle()
        {
            // 0：异常失败  1：成功
            int flag = 0;
            try
            {
                string title = Request["title"];
                string a = Request["a"];
                string b = Request["b"];
                string c = Request["c"];
                string d = Request["d"];
                string key = Request["key"];
                string analysis = Request["analysis"];

                if (Session["account"] != null)
                {
                    FrSingle single = new FrSingle();

                    single.Title = title;
                    single.A = a;
                    single.B = b;
                    single.C = c;
                    single.D = d;
                    single.Answer = key;
                    single.Analysis = analysis;
                    single.AddPerson = AdminManager.GetAdminName(Session["account"].ToString());
                    single.AddTime = DateTime.Now;

                    single.Save();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("添加财经法规单选题失败", ex);
            }

            return Content(flag.ToString(), MIME_TYPE);
        }

        // 删除单选题
        public ActionResult DeleteSingle()
        {
            // 0:异常失败  1:成功
            int flag = 0;
            try
            {
                string id = Request["id"];
                FrSingle single = FrSingle.GetSingleByID(new Guid(id));
                if (single != null)
                {
                    single.Delete();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("删除财经法规单选题失败", ex);
            }
            return Content(flag.ToString(), MIME_TYPE);
        }

        // 修改单选题
        public ActionResult UpdateSingle()
        {
            // 0:异常失败  1:成功
            int flag = 0;
            try
            {
                if (Session["account"] == null)
                {
                    string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                    return Content(script);
                }

                string id = Request["id"];
                FrSingle single = FrSingle.GetSingleByID(new Guid(id));
                if (single != null)
                {
                    string title = Request["title"];
                    string a = Request["a"];
                    string b = Request["b"];
                    string c = Request["c"];
                    string d = Request["d"];
                    string key = Request["key"];
                    string analysis = Request["analysis"];

                    single.Title = title;
                    single.A = a;
                    single.B = b;
                    single.C = c;
                    single.D = d;
                    single.Answer = key;
                    single.Analysis = analysis;

                    single.Save();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("修改财经法规单选题失败", ex);
            }
            return Content(flag.ToString(), MIME_TYPE);
        }

        // 查询单选题
        public ActionResult QuerySingle()
        {
            string json = EasyuiHelper.DefaultJson;
            try
            {
                string title = Request["title"];
                string a = Request["a"];
                string b = Request["b"];
                string c = Request["c"];
                string d = Request["d"];
                string key = Request["key"];
                string analysis = Request["analysis"];

                // 全部搜索
                int currentPage = 1;
                string pageSize = Request["rows"];

                // 拼接sql基本查询字段
                string sql_base = "select * from Fr_Single";
                string sql_where = string.Empty;
                if (!string.IsNullOrEmpty(title))
                {
                    sql_where += string.Format("and title like '%{0}%' ", title);
                }
                if (!string.IsNullOrEmpty(a))
                {
                    sql_where += string.Format("and answer_a like '%{0}%' ", a);
                }
                if (!string.IsNullOrEmpty(b))
                {
                    sql_where += string.Format("and answer_b = '{0}' ", b);
                }
                if (!string.IsNullOrEmpty(c))
                {
                    sql_where += string.Format("and answer_c like '%{0}%' ", c);
                }
                if (!string.IsNullOrEmpty(d))
                {
                    sql_where += string.Format("and answer_d like '%{0}%' ", d);
                }
                if (!string.IsNullOrEmpty(key) && key.Trim() != "E")
                {
                    sql_where += string.Format("and answer_key = '{0}' ", key);
                }
                if (!string.IsNullOrEmpty(analysis))
                {
                    sql_where += string.Format("and analysis like '%{0}%' ", analysis);
                }

                // 有查询条件，没有查询所有
                string sql = sql_base;
                if (sql_where.Length != 0)
                {
                    // 去掉第一个"and"
                    if ((sql_where.TrimStart()).StartsWith("and"))
                    {
                        sql_where = sql_where.Substring(3, sql_where.Length - 3);
                        sql = sql_base + " where " + sql_where;
                    }
                }
                json = FrSingleManager.GetAllSingle(currentPage, int.Parse(pageSize), sql);
            }
            catch (Exception ex)
            {
                json = EasyuiHelper.DefaultJson;
                LogHelper.WriteLogAsync("查询财经法规单选题错误", ex);
            }
            return Content(json, MIME_TYPE);
        }

        #endregion
    }
}
